package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.TestMediaPlayerActivity;
import reduce.fz.com.reduce.activity.UserDetailActivity;
import reduce.fz.com.reduce.activity.UserDetailActivity2;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.event.ShowPubVideoEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ScreenUtils;

public class FindVideoFragment extends BaseListFragment<FindVideoFragment.FindVideoViewHolder, VideoCard> {

    //0最近，1热门
    private int type = 0;

    public static FindVideoFragment create(int type) {
        FindVideoFragment fragment = new FindVideoFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    public interface VideoCardListener {
        public void VideoThumClick(VideoCard card);

        public void clickUser(User user);
    }

    /**
     * 用户详情页面设定
     *
     * @param user
     */
    public void forwardUserDetail(User user) {
        Intent intent = new Intent(getActivity(), UserDetailActivity2.class);
        intent.putExtra("uid", user.uid);
        getActivity().startActivity(intent);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        type = getArguments().getInt("type");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 坚挺最新
     *
     * @param event
     */
    public void onEvent(ShowPubVideoEvent event) {
        if (type == 0) {
            addFrist(event.card);
        }
    }

    public FindVideoFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        return layoutManager;
    }

    @Override
    protected FindVideoViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_video_find, parent, false);
        FindVideoViewHolder viewHolder = new FindVideoViewHolder(view, new VideoCardListener() {
            @Override
            public void VideoThumClick(VideoCard card) {
                forwardDetail(card.cardid);
            }

            @Override
            public void clickUser(User user) {
                forwardUserDetail(user);
            }
        });
        return viewHolder;
    }

    public void forwardDetail(Long cardid) {
        Intent intent = new Intent(getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", cardid);
        startActivity(intent);
    }

    public void forwardTExt(Long cardid) {
        Intent intent = new Intent(getActivity(), TestMediaPlayerActivity.class);
        startActivity(intent);
    }


    @Override
    protected FindVideoViewHolder createfootViewHolder(View view) {
        return new FindVideoViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<VideoCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_show_video, type, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }


    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<VideoCard>>(new TypeReference<Result<List<VideoCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();

            }

            @Override
            public void responseInfo(Result<List<VideoCard>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected int getPageType() {
        return 0;
    }


    public static class FindVideoViewHolder extends BaseRecycleViewHolder<VideoCard> implements View.OnClickListener {
        private NetworkImageView mVideoThum;
        private RoundNetworkImageView mUserHeader;
        private TextView mTvNickName;
        private VideoCardListener listener;
        private VideoCard card;

        public FindVideoViewHolder(View itemView, VideoCardListener listener) {
            this(itemView);
            this.listener = listener;
        }

        public FindVideoViewHolder(View itemView) {
            super(itemView);
            mVideoThum = (NetworkImageView) itemView.findViewById(R.id.niv_video_thum);
            mUserHeader = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_header);
            mTvNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            if (mUserHeader != null) {
                mUserHeader.setOnClickListener(this);
            }
        }

        /**
         * 获取图片宽度
         *
         * @return
         */
        public int getWidth() {
            int width = ScreenUtils.getScreenW() / 2;
            return width;
        }


        @Override
        public void renderView(final VideoCard data, int position) {

            if (data == null) return;
            this.card = data;
            if (data.video.thumUrl != null)
                mVideoThum.setImageUrl(data.video.thumUrl, HttpUtils.getImageLoader());
            else {
                mVideoThum.setImageUrl(null,HttpUtils.getImageLoader());
            }
            if (data.user != null) {
                mUserHeader.setImageUrl(data.user.avatar, HttpUtils.getAvatarImageLoader());
                mTvNickName.setText(data.user.nickname);
            }else{
                mUserHeader.setImageUrl(null,HttpUtils.getAvatarImageLoader());
                mTvNickName.setText("");
            }
            mVideoThum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.VideoThumClick(data);
                }
            });
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(getWidth(), getWidth());
            mVideoThum.setLayoutParams(lp);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_user_header:
                    listener.clickUser(card.user);
                    break;
                default:
                    break;
            }

        }
    }


}
